﻿//如何统计文本的行数及如何读取文件某一行内容：
 
#include <iostream>
#include <fstream>
#include <string>

#include <boost/filesystem.hpp>

#include <Common/Logger.h>
#include <Common/TimeHelper.h>

#include "CountLines-Config.h"

#define STR1(R) #R
#define STR2(R) STR1(R)

using namespace std;
 
int CountLines(char *filename)
{
    ifstream ReadFile;
    int n=0;
    string tmp;
    ReadFile.open(filename,ios::in);//ios::in 表示以只读的方式读取文件
    if(ReadFile.fail())//文件打开失败:返回0
    {
        return 0;
    }
    else//文件存在
    {
        while(getline(ReadFile,tmp,'\n'))
        {
            n++;
        }
        ReadFile.close();
        return n;
    }
}

int CountLines(std::string filename, int &take_time)
{
    ifstream ReadFile;
    int n = 0;
    string tmp;
    
    TimeHelper time;
    int start = time.toc();
    take_time = 0;

    ReadFile.open(filename, ios::in);//ios::in 表示以只读的方式读取文件
    if (ReadFile.fail())//文件打开失败:返回0
    {
        return 0;
    }
    else//文件存在
    {
        while (getline(ReadFile, tmp, '\n'))
        {
            n++;
        }
        ReadFile.close();
        int finish = time.toc();
        //LOG_D("cout lines takes {} ms", finish - start);
        take_time = finish - start;
        return n;
    }
}
 
 string ReadLine(char *filename,int line)
 {
     int lines,i=0;
     string temp;
     fstream file;
     file.open(filename,ios::in);
     lines=CountLines(filename);
 
     if(line<=0)
     {
         return "Error 1: 行数错误，不能为0或负数。";
     }
     if(file.fail())
     {
         return "Error 2: 文件不存在。";
     }
     if(line>lines)
     {
         return "Error 3: 行数超出文件长度。";
     }
     while(getline(file,temp)&&i<line-1)
     {
         i++;
     }
     file.close();
     return temp;
 }

string ReadLine(string filename, int line, int max_lines)
{
    int i = 0, times = 0;
    string temp;
    fstream file;
    file.open(filename, ios::in);
    //lines = CountLines(filename, times);

    if (line <= 0)
    {
        return "Error 1: 行数错误，不能为0或负数。";
    }
    if (file.fail())
    {
        return "Error 2: 文件不存在。";
    }
    if (line > max_lines)
    {
        return "Error 3: 行数超出文件长度。";
    }
    while (getline(file, temp) && i < line - 1)
    {
        i++;
    }
    file.close();
    return temp;
}

int main()
{
    LOG_I("the current path is {}\n", STR2(CURRENT_PATH));
    std::string current_path = STR2(CURRENT_PATH);
    LOG_I("the current path is {}", current_path);
    //boost::filesystem::path current_path = boost::filesystem::initial_path<boost::filesystem::path>();
    //LOG_I("the current path is {}", current_path.string());

    int max_lines, line, cout_time;
    //char filename[]="./1.txt";
    std::string filename = current_path + "/1.txt";

    max_lines = CountLines(filename, cout_time);
    LOG_I("count lines is {}, and takes {} ms", max_lines, cout_time);
    
    /*cout<<"请输入要读取的行数:"<<endl;
    while(cin >> line)
    {
        TimeHelper times;
        int start = times.toc();
        cout << "第" << line << "行的内容是 ：" << endl;
        cout << ReadLine(filename, line, max_lines);
        cout << "耗费时间为：" << times.toc() - start << endl;
        cout << "\n\n请输入要读取的行数:" << endl;
    }*/

    TimeHelper time;
    int start = time.toc();
    
    ifstream test_fin(filename, std::ios::binary); 
    vector<char> buf(test_fin.seekg(0, std::ios::end).tellg());
    test_fin.seekg(0, std::ios::beg).read(&buf[0], static_cast<std::streamsize>(buf.size()));
    test_fin.close();
    LOG_I("耗费时间为 {} ms", time.toc() - start);
    LOG_I("buf size is {}", buf.size());

    for (auto i : buf) {
        if (i < '0' || i > '9')
            cout << endl;
        cout << i ;
    }
        

    return 0;
}

